package task3.threshold;

import java.awt.image.renderable.ParameterBlock;
import java.security.InvalidParameterException;
import javax.media.jai.JAI;
import task3.pimpmypipe.DataTransformationFilter;
import task3.util.FilterObservable;
import task3.util.ImageTO;

public class ThresholdFilter
  extends DataTransformationFilter<ImageTO>
{
  private FilterObservable _observable;
  private int _low;
  private int _high;
  private int _map;
  
  public ThresholdFilter()
    throws InvalidParameterException
  {
    this._observable = new FilterObservable();
    this._low = 0;
    this._high = 50;
    this._map = 255;
  }
  
  protected void process(ImageTO entity)
  {
    int bands = 3;
    double[] low = new double[bands];
    double[] high = new double[bands];
    double[] map = new double[bands];
    for (int i = 0; i < bands; i++)
    {
      low[i] = this._low;
      high[i] = this._high;
      map[i] = this._map;
    }
    ParameterBlock parameters = new ParameterBlock();
    parameters.addSource(entity.getEditedImage());
    parameters.add(low);
    parameters.add(high);
    parameters.add(map);
    
    entity.setEditedImage(JAI.create("threshold", parameters));
    
    this._observable.setChanged();
    this._observable.notifyObservers(entity);
  }
  
  public FilterObservable getObservable()
  {
    return this._observable;
  }
  
  public int getLow()
  {
    return this._low;
  }
  
  public void setLow(int low)
  {
    this._low = low;
  }
  
  public int getHigh()
  {
    return this._high;
  }
  
  public void setHigh(int high)
  {
    this._high = high;
  }
  
  public int getMap()
  {
    return this._map;
  }
  
  public void setMap(int map)
  {
    this._map = map;
  }
}
